package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    /*
        员工登录
     */
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee){
        //将页面密码进行md5加密
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        
        //根据用户名查询数据库
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername,employee.getUsername());
        Employee emp = employeeService.getOne(queryWrapper);

        //判断查询结果为空时返回登录失败的结果
        if(emp  == null){
            return R.error("当前用户未注册");
        }
        //查询到用户，进行密码比对失败返回结果
        if(!emp.getPassword().equals(password)){
            return R.error("用户名或密码错误,请尝试重新登录");
        }
        //查询用户，若被禁用则返回结果
        if(emp.getStatus() == 0){
            return R.error("该用户已被禁用");
        }

        //登录成功,将员工数据存入session中
        request.getSession().setAttribute("employee",emp.getId());
        return R.success(emp);
    }
    /*
        登出账户
     */
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        //删除session
        request.getSession().removeAttribute("employee");
        return R.success("已退出当前用户");
    }

    /*
        添加员工
     */
    @PostMapping
    public R<String> save(HttpServletRequest request,@RequestBody Employee employee){
        //设置员工初始密码---md5加密
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        //添加初始创建日期，与添加人
        /*
        改为公共字段自动填充
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());

        Long createId = (Long) request.getSession().getAttribute("employee");

        employee.setCreateUser(createId);
        employee.setUpdateUser(createId);
         */

        //提交
        employeeService.save(employee);

        return R.success("新增用户成功");
    }

    /*
        员工信息分页查询
     */
    @GetMapping("/page")
    public R<Page>  page(int page,int pageSize,String name){
        //构造分页构造器
        Page pageInfo = new Page<>(page,pageSize);

        //构造条件构造器
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper();
        //添加过滤条件
        queryWrapper.like(StringUtils.isNotEmpty(name),Employee::getName,name);
        //进行排序
        queryWrapper.orderByDesc(Employee::getUpdateTime);

        //执行查询
        employeeService.page(pageInfo,queryWrapper);
        return R.success(pageInfo);
    }

    /*
        修改员工信息
     */
    @PutMapping
    public R<String> update(HttpServletRequest request,@RequestBody Employee employee){
        /* 公共字段自动填充
        Long empId = (Long) request.getSession().getAttribute("employee");
        employee.setUpdateUser(empId);
        employee.setUpdateTime(LocalDateTime.now());

         */
        employeeService.updateById(employee);

        return R.success("修改成功");
    }

    /*
       根据id查找员工
     */
    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable Long id){
        Employee employee = employeeService.getById(id);
        if(employee != null){
            return R.success(employee);
        }
        return R.error("未查找到对应员工");
    }
}
